package action;

import global.Constants;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.AccountCategoryService;
import util.ServletHelp;
import util.StringUtils;
import domain.AccountCategory;

/**
 * 账目分类Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AccountCategoryAction extends BaseAction {
	/**
	 * 收支项目Service
	 * */
	private AccountCategoryService accountCategoryService = null;

	/**
	 * @param accountCategoryService
	 *            the accountCategoryService to set
	 */
	public void setAccountCategoryService(AccountCategoryService accountCategoryService) {
		this.accountCategoryService = accountCategoryService;
	}

	/**
	 * 收支项目查询方法
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 参数
		AccountCategory paramAccountCategory = new AccountCategory();
		paramAccountCategory.setStatus(request.getParameter("status"));
		// 取得收支项目列表
		List<AccountCategory> accountCategoryList = new ArrayList<AccountCategory>();
		JSONArray datas = new JSONArray();
		accountCategoryList = accountCategoryService.selectByCriteria(paramAccountCategory);
		if(null!=accountCategoryList){
			datas = JSONArray.fromObject(accountCategoryList);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	
	/**
	 * 收支项目查询方法 带分页
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward queryForPaging(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 分页参数
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		AccountCategory paramAccountCategory = new AccountCategory();
		paramAccountCategory.setStart(Integer.parseInt(start));
		paramAccountCategory.setLimit(Integer.parseInt(limit));
		// 取得收支项目列表
		List<AccountCategory> accountCategoryList = new ArrayList<AccountCategory>();
		JSONArray datas = new JSONArray();
		accountCategoryList = accountCategoryService.selectByCriteriaForPaging(paramAccountCategory);
		if(null!=accountCategoryList){
			datas = JSONArray.fromObject(accountCategoryList);
		}
		int count = accountCategoryService.countByCriteria(paramAccountCategory);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}


	/**
	 * 保存收支项目
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得参数
		String jsonArr = request.getParameter("jsonArr");
		if(StringUtils.isNotEmpty(jsonArr)){
			JSONArray accountCategoryArr = JSONArray.fromObject(jsonArr);
			for(int i=0;i<accountCategoryArr.size();i++){
				JSONObject accountCategoryObj = accountCategoryArr.getJSONObject(i);
				AccountCategory accountCategory = new AccountCategory();
				accountCategory.setCategoryName(accountCategoryObj.getString("categoryName"));
				accountCategory.setDelflag(Constants.DEL_FLAG_1);
				accountCategory.setStatus(accountCategoryObj.getString("status"));
				if(StringUtils.isNotEmpty(accountCategoryObj.getString("categoryName"))){
					if(StringUtils.isNotEmpty(accountCategoryObj.getString("categoryId"))){
						accountCategory.setCategoryId(Integer.parseInt(accountCategoryObj.getString("categoryId")));
						// 更新收支项目
						accountCategoryService.update(accountCategory);
					}else{
						// 添加收支项目
						accountCategoryService.insert(accountCategory);
					}
				}
			}
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 删除收支项目
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得参数
		String accountCategoryIdStr = request.getParameter("accountCategoryIds");
		
		// 删除收支项目
		accountCategoryService.delete(accountCategoryIdStr);
		
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
